<template>
  <div class="page">
    <!-- <el-form :model="searchForm" label-position="left" inline label-suffix="：" size="small">
      <div class="flex wap flex-start">
        <el-form-item label="名称">
          <el-input v-model="searchForm.name"></el-input>
        </el-form-item>
        <el-form-item label="手机号码">
          <el-input v-model="searchForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="">
          <el-button type="primary" @click="getData">查询</el-button>
        </el-form-item>
      </div>
    </el-form> -->
    <div class="flex wap flex-start" style="padding: 0px 0 10px 0">
      <el-button type="success" @click="add">新增</el-button>
      <div style="width:20px"></div>
      <!-- <el-button type="danger" @click="del">删除</el-button> -->
    </div>
    <el-table :data="tableData" border style="width: 100%"  height="600"  :header-cell-style="{
        background: '#eef1f6',
        color: '#606266',
        'text-align': 'center'
      }"
      :cell-style="{ 'text-align': 'center' }">
      <el-table-column type="selection"></el-table-column>
      <!-- <el-table-column prop="tenantId" label="租户ID"></el-table-column> -->
      <el-table-column prop="tcode" label="机构代码"></el-table-column>
      <el-table-column prop="tenantName" label="机构名称"></el-table-column>
      <el-table-column prop="contact" label="联系人"></el-table-column>
      <el-table-column prop="phone" label="联系电话"></el-table-column>
      <el-table-column prop="createtime" label="创建时间"></el-table-column>
      <el-table-column prop="endtime" label="到期时间"></el-table-column>
      <el-table-column prop="isdel" label="状态" width="100">
        <template slot-scope="scop">
          <el-tag type="success" v-if="scop.row.status == 1">正常</el-tag>
          <el-tag type="warning" v-if="scop.row.status == 2">暂停</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="" label="操作" width="120">
        <template slot-scope="scop">
          <el-button type="primary" icon="el-icon-edit" circle @click="edit(scop.row)"></el-button>
          <el-button type="danger" icon="el-icon-delete" circle @click="del(scop.row.tenantId)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="float:right;padding:10px">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :page-sizes="[10, 20, 50, 100, 200]"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>
    <el-dialog title="新增/修改机构" center :visible.sync="dialogFormVisible" width="40%">
      <el-form :model="addOrEditForm" ref="RefAddOrEditForm" :rules="rule" label-width="120px" class="edit-form"
        label-suffix="：">
        <el-form-item label="机构代码" prop="tcode">
          <el-input type="text" placeholder="请输入机构代码" v-model="addOrEditForm.tcode"></el-input>
        </el-form-item>
        <el-form-item label="机构名称" prop="tenantName">
          <el-input type="text" placeholder="请输入机构名称" v-model="addOrEditForm.tenantName"></el-input>
        </el-form-item>
        <el-form-item label="联系人" prop="contact">
          <el-input type="text" placeholder="请输入联系人" v-model="addOrEditForm.contact"></el-input>
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input type="text" placeholder="请输入联系电话" maxlength="11" v-model="addOrEditForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="到期时间" prop="endtime">
          <el-date-picker v-model="addOrEditForm.endtime" :picker-options="options" type="date" placeholder="选择日期"
            value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
        <!-- <el-form-item label="封面" prop="img">
          <upload v-model="addOrEditForm.img" />
        </el-form-item>-->
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="addOrEditForm.status">
            <el-radio :label="2">暂停</el-radio>
            <el-radio :label="1">正常</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="短信服务">
          <el-switch v-model="isMsg" active-text="开启" inactive-text="不开启">
          </el-switch>
        </el-form-item>
        <div v-if="isMsg">
          <el-row type="flex" justify="">
            <el-col :span="12">
              <el-form-item label="SecretId" prop="secretid">
                <el-input type="text" placeholder="请输入SecretId" v-model="addOrEditForm.secretid"></el-input>
              </el-form-item>
              <el-form-item label="SecretKey" prop="secretkey">
                <el-input type="text" placeholder="请输入SecretKey" v-model="addOrEditForm.secretkey"></el-input>
              </el-form-item>
              <el-form-item label="SDKAppId" prop="sdkappid">
                <el-input type="text" placeholder="请输入SDKAppId" v-model="addOrEditForm.sdkappid"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="短信签名" prop="signname">
                <el-input type="text" placeholder="请输入短信签名" v-model="addOrEditForm.signname"></el-input>
              </el-form-item>
              <el-form-item label="短信模板id①" prop="templateid1">
                <el-input type="text" placeholder="请输入短信模板id①" v-model="addOrEditForm.templateid1"></el-input>
              </el-form-item>
              <el-form-item label="模板id②" prop="templateid2">
                <el-input type="text" placeholder="请输入模板id②" v-model="addOrEditForm.templateid2"></el-input>
              </el-form-item>
            </el-col>
          </el-row>

        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { findTenantList, addTenant, updateTenant, delTenant } from "@/api/user";
import upload from "@/components/uploadFile";
export default {
  components: {
    upload
  },
  data() {
    return {
      isMsg: false,
      total: 0,
      pageNum: 1,
      pageSize: 10,
      tableData: [],
      searchForm: {
        name: "",
        phone: ""
      },
      dialogFormVisible: false,
      addOrEditForm: {
        tenantId: '',
        tcode: '',
        tenantName: '',
        contact: '',
        phone: '',
        endtime: '',
        status: 1,
        secretid: '',
        secretkey: '',
        sdkappid: '',
        signname: '',
        templateid1: '',
        templateid2: ''
      },
      options: {
        disabledDate: (time) => {
          return time.getTime() < new Date().getTime()
        }
      }
    };
  },
  computed: {
    queryData() {
      return {
        pageNum: this.pageNum,
        pageSize: this.pageSize
      };
    },
    rule() {
      return {
        tcode: [
          { required: true, message: '请设置机构代码', trigger: 'change' }
        ],
        tenantName: [
          { required: true, message: '请设机构名称', trigger: 'change' }
        ],
        contact: [
          { required: true, message: '请填写联系人', trigger: 'change' }
        ],
        phone: [
          { required: true, message: '请填写联系电话', trigger: 'change' },
          { pattern: this.$reg.reg_phone, message: '手机号格式错误', trigger: 'change' },
        ],
        endtime: [
          { required: true, message: '请选择到期时间', trigger: 'change' }
        ],
        secretid: [
          { required: this.isMsg, message: '请填写SecretId', trigger: 'change' }
        ],
        secretkey: [
          { required: this.isMsg, message: '请填写SecretKey', trigger: 'change' }
        ],
        sdkappid: [
          { required: this.isMsg, message: '请填写SDKAppId', trigger: 'change' }
        ],
        templateid1: [
          { required: this.isMsg, message: '请填写短信模板id①', trigger: 'change' }
        ],
        templateid2: [
          { required: this.isMsg, message: '请填写模板id②', trigger: 'change' }
        ],
        signname: [
          { required: this.isMsg, message: '请填写短信签名', trigger: 'change' }
        ]
      }
    }
  },
  mounted() {
    this.getData();
  },
  methods: {
    async getData() {
      const res = await findTenantList.post(this.queryData);
      if (res.resultCode === "000000") {
        this.tableData = res.data.tenantList;
        this.total = res.data.count;
      } else {
        this.$message.error(res.resultInfo);
      }
    },
    edit(row) {
      this.dialogFormVisible = true
      const { tenantId, tcode, tenantName, contact, phone, endtime, status, secretid, secretkey, sdkappid, signname, templateid1, templateid2 } = row
      this.addOrEditForm = {
        tenantId,
        tcode,
        tenantName,
        contact,
        phone,
        endtime,
        status,
        secretid,
        secretkey,
        sdkappid,
        signname,
        templateid1,
        templateid2
      }
    },
    add() {
      this.dialogFormVisible = true
      this.$refs.RefAddOrEditForm.resetFields()
      this.addOrEditForm = {
        tenantId: '',
        tcode: '',
        tenantName: '',
        contact: '',
        phone: '',
        endtime: '',
        status: 1,
        secretid: '',
        secretkey: '',
        sdkappid: '',
        signname: '',
        templateid1: '',
        templateid2: ''
      }
    },
    save() {
      this.$refs.RefAddOrEditForm.validate(async (valid) => {
        if (valid) {
          if (this.addOrEditForm.tenantId === '') {
            const res = await addTenant.post(this.addOrEditForm);
            if (res.resultCode === "000000") {
              this.$message.success(res.resultInfo);
              this.dialogFormVisible = false
              this.getData()
            } else {
              this.$message.error(res.resultInfo);
            }
          } else {
            const res = await updateTenant.post(this.addOrEditForm);
            if (res.resultCode === "000000") {
              this.$message.success(res.resultInfo);
              this.dialogFormVisible = false
              this.getData()
            } else {
              this.$message.error(res.resultInfo);
            }
          }

        } else {
          return false;
        }
      });
    },
    del(tenantId) {
      this.$confirm("此操作将删除该项目, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.delImg(tenantId)
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    async delImg(id) {
      const res = await delTenant.post({ tenantId: id });
      if (res.resultCode === "000000") {
        this.$message.success("删除成功！");
        this.getData();
      } else {
        this.$message.error(res.resultInfo);
      }
    },
    handleSizeChange(val) {
      this.pageNum = 1;
      this.pageSize = val;
      this.getData();
    },
    handleCurrentChange(val) {
      this.pageNum = val;
      this.getData();
    }
  }
};
</script>
<style lang="less" scoped></style>
